package check.x.com;

import check.x.com.utils.SsoChecks;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created by sanmao on 2017/6/12.
 */
public class LoginAction extends ActionSupport{

    private String username;
    private String password;
    private String gotoUrl;
    private String cookieName;
    private String cookieValue;

    public String getCookieName() {
        return cookieName;
    }

    public void setCookieName(String cookieName) {
        this.cookieName = cookieName;
    }

    public String getCookieValue() {
        return cookieValue;
    }

    public void setCookieValue(String cookieValue) {
        this.cookieValue = cookieValue;
    }

    public String doLogin(){
        boolean bool= SsoChecks.checkLogin(username,password);
        if(bool){
            Cookie cookie=new Cookie("logincookie","ssohxw");
            cookie.setDomain(".com");
            cookie.setPath("/");
            HttpServletResponse response= ServletActionContext.getResponse();
            response.addCookie(cookie);

            return SUCCESS;
        }

        return null;
    }

    public void checkCookie() throws IOException {
        boolean ok=SsoChecks.checkCookie(cookieName,cookieValue);
        String result="0";
        if(ok){
            result="1";
        }
        HttpServletResponse response=ServletActionContext.getResponse();
        response.getWriter().print(result);
        response.getWriter().close();
    }


    public String getGotoUrl() {
        return gotoUrl;
    }

    public void setGotoUrl(String gotoUrl) {
        this.gotoUrl = gotoUrl;
    }


    public String getUsername() {

        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


}
